﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;
using SlimDX.Direct3D9;

namespace Terrain
{
	class SimpleMeshObject
	{
		Device device;

		MeshObject mesh;
		Effect effect;

		Matrix world;

		public SimpleMeshObject(Device device, float scale)
		{
			this.device = device;
			mesh = new MeshObject(device);
			mesh.LoadMesh("czolgv2.x");
			world = Matrix.Scaling(scale, 2 * scale, scale) * Matrix.Translation(0, -5, 0);

			effect = Effect.FromFile(device, "../../simple.fx", ShaderFlags.None);
		}

		public void Render()
		{
			Matrix worldViewProj = world * device.GetTransform(TransformState.View) * device.GetTransform(TransformState.Projection);

			device.SetStreamSource(0, mesh.mesh.VertexBuffer, 0, mesh.mesh.BytesPerVertex);
			device.Indices = mesh.mesh.IndexBuffer;
			device.VertexFormat = mesh.mesh.VertexFormat;

			effect.Technique = "Simple";
			effect.SetValue("xWorldViewProj", worldViewProj);
			effect.SetTexture("xTerrainTexture", mesh.textures[0]);

			effect.Begin(0);
			effect.BeginPass(0);
			{
				device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.mesh.VertexCount, 0, mesh.mesh.FaceCount);
			}
			effect.EndPass();
			effect.End();
		}
	}
}
